<template>
  <!-- 列表 -->
  <ContentWrap>
    <el-button
      v-hasPermi="['deviceinfo:deviceinfo:create']"
      plain
      type="primary"
      @click="openForm('create')"
    >
      <Icon class="mr-5px" icon="ep:plus" />
      新增
    </el-button>

    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      
       <el-table-column label="设备类型" align="center" prop="sblx" width="150px">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.DEVICE_TYPE" :value="scope.row.sblx" />
        </template>
      </el-table-column>
      <el-table-column label="设备编号" align="center" prop="sbid" width="150px"/>
      <el-table-column label="设备名称" align="center" prop="sbmc" /> 

      <el-table-column label="设备所属部门" align="center" prop="ssbm" />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['deviceinfo:deviceinfo:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['deviceinfo:deviceinfo:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </ContentWrap>

    <!-- 表单弹窗：添加/修改 -->
    <deviceinfoForm ref="formRef" :gjbh="gjbh"  @success="getList" />
</template>
<script setup lang="ts">
import { DICT_TYPE } from '@/utils/dict'
import { intersectionApi } from '@/api/intersection/intersection'
import deviceinfoForm from '../../../deviceinfo/deviceinfo/deviceinfoForm.vue'
import { deviceinfoApi, deviceinfoVO } from '@/api/deviceinfo/deviceinfo'
const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const props = defineProps<{
  gjbh?: number // 所在杆件（主表的关联字段）
}>()
const loading = ref(false) // 列表的加载中
const list = ref([]) // 列表的数据

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    list.value = await intersectionApi.getbarinfoListBygjbh(props.gjbh)
  } finally {
    loading.value = false
  }
}
const formRef = ref()
const openForm = (type: string, id?: number) => {
  if (!props.gjbh) {
    message.error('请选择一个杆件进行添加')
    return
  }
  formRef.value.open(type, id, props.gjbh)
}


/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await deviceinfoApi.deletedeviceinfo(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}


/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>